#ifndef __OS_CONFIG_H__
#define __OS_CONFIG_H__

// #include "rtdef.h"
// #include "rtdbg.h"
// #include "mqueue.h"


#include "os_api.h"

// #include "bd_queue.h"
// #include "error_functions.h"
// //#include "getticks.h"
// #include "kal_string.h"
// #include "malloc_my.h"
#include "msgqueue_v2.h"
#include "my_events.h"
#include "os_timer.h"
// #include "pthread_param_custom.h"
// #include "resource_api.h"
// #include "singlequeue.h"
#include "sleep_fun.h"

// #include "threads_signal_param.h"


#include "os_include.h"


// #ifdef AK_RTOS

// #define __RTT_OS__ 1
// #endif

typedef uint32_t kal_uint32;


#if defined(__FreeRTOS__)
#define SYSTEM_HZ   configTICK_RATE_HZ  // 1000
#elif defined(__RTT_OS__)
#define SYSTEM_HZ   RT_TICK_PER_SECOND
#else
#define SYSTEM_HZ   TIMER0_HZ 
//#define CUSTOM_TIMER1_HZ       10000 // 10K
#endif


#if defined(__FreeRTOS__)
#define OS_MAX_TICK  portMAX_DELAY
#define INT_ENTER()  
#define INT_EIXT()   

#define  SYSTEM_CRITICAL_ENTER()  portENTER_CRITICAL()
#define  SYSTEM_CRITICAL_LEAVE()  portEXIT_CRITICAL()

#elif defined(__RTT_OS__)
#define OS_MAX_TICK  RT_WAITING_FOREVER

#define INT_ENTER()  // rt_interrupt_enter()   -->  rt_hw_trap_irq
#define INT_EIXT()   // rt_interrupt_leave()

#define  SYSTEM_CRITICAL_ENTER()  rt_enter_critical()      // 
#define  SYSTEM_CRITICAL_LEAVE()  rt_exit_critical()

#else


#define INT_ENTER()  
#define INT_EIXT() 


#define  SYSTEM_CRITICAL_ENTER()  CUSTOM_ENTER_CRITICAL()
#define  SYSTEM_CRITICAL_LEAVE()  CUSTOM_EXIT_CRITICAL()

#endif



#define SYS_NONCACHE_BIT	0x80000000




// 1000 Hz
#define kal_ticks_to_milli_secs(ticks)   (ticks) 
#define kal_milli_secs_to_ticks(ms)      (ms)
#define kal_secs_to_ticks(sec)            ((sec)*1000)
#define kal_ticks_to_secs(ticks)			((ticks)/1000)



#if defined(__FreeRTOS__)
#define kal_get_systicks() 			xTaskGetTickCount_custom() //	xTaskGetTickCount()  // xTaskGetTickCountFromISR
#define delay_ms(ms)             vTaskDelay(kal_milli_secs_to_ticks(ms))

extern bool systemIsISR(void);

#define get_ctrl_buffer(X)     pvPortMalloc(X) //  pvPortMalloc(X)
//#define get_ctrl_buffer(X)     (trace_hw_print("mc:%d;%s,%d\r\n",X, __FUNCTION__, systemIsISR()),pvPortMalloc(X) )

#define free_ctrl_buffer(X) 	vPortFree(X)  //	vPortFree(X)
//#define free_ctrl_buffer(X) 	 {trace_hw_print("free:0x%p;%s,%d\r\n",X, __FUNCTION__, __LINE__);vPortFree(X);} // debug 


#elif defined(__RTT_OS__)

#define TASK_STACK_MIN	(512)
#define TASK_PRIO_M		 10

#define WAIT_KEY()   do{ printf("\nwait key\n");getchar();}while(0)


#if defined(RT_USING_MEM_MUTEX)
extern void *rt_malloc_mutex(rt_size_t size);
extern void rt_free_mutex(void *rmem);
#endif

extern void rt_free_ext(void *pprmem, const char *func, rt_size_t line);
extern void *rt_malloc_ext(rt_size_t size, const char *func, rt_size_t line);
extern void *rt_calloc_ext(rt_size_t count, rt_size_t size, const char *func, rt_size_t line);
extern void *rt_realloc_ext(void *rmem, rt_size_t newsize, const char *func, rt_size_t line);

#define kal_get_systicks() 				rt_tick_get()
#define delay_ms(ms)             rt_thread_mdelay(ms)        //  rt_thread_sleep

#define get_ctrl_buffer(X) 		rt_malloc(X)
#define free_ctrl_buffer(X) 		rt_free(X)

//#define get_ctrl_buffer(X) 		rt_malloc_ext(X,__FUNCTION__,__LINE__)
//#define free_ctrl_buffer(X) 	rt_free_ext(X,__FUNCTION__,__LINE__)

#else
#define kal_get_systicks() 				sysGetTicks(TIMER0)
#define delay_ms(ms)             sysDelay(kal_milli_secs_to_ticks(ms))

#define get_ctrl_buffer(X) malloc(X)
#define free_ctrl_buffer(X) free(X)


#endif


#define OslMalloc(nob)  get_ctrl_buffer(nob)
#define OslMfree(frp)   free_ctrl_buffer(frp)

#if defined(__RTT_OS__)
#define OslRealloc(rmem,newsize) rt_realloc(rmem,newsize)
#define OslCalloc(count,size) rt_calloc(count,size)

//#define OslRealloc(rmem,newsize) rt_realloc_ext(rmem,newsize,__FUNCTION__,__LINE__)
//#define OslCalloc(count,size) rt_calloc_ext(count,size,__FUNCTION__,__LINE__)
#endif


#if !defined(__PC_SIM__)
#define delay_ms_hw(ms)           sysDelay(kal_milli_secs_to_ticks(ms))
#endif

extern void delay_ms_sys(uint32_t nms);

#if defined(__PC_SIM__)

#if defined(__RTT_OS__)
#define DBG_HW_PRINTF  rt_kprintf
#else
#define DBG_HW_PRINTF   printf
#endif

#elif defined(__RTT_OS__)
#define DBG_HW_PRINTF  rt_kprintf
#elif defined(__FreeRTOS__)
#define DBG_HW_PRINTF   trace_hw_print //sysprintf
#else
#define DBG_HW_PRINTF   sysprintf
#endif

#define  kal_sleep_task(ms) delay_ms(ms)


//extern void trace_hw_print(const char *form ,...);

extern void vid_nvram_updata_req(void); 
extern void fst_rf_nvram_updata(void);

#if defined(__PC_SIM__)
#define NVRAM_UPDATA()   DBG_HW_PRINTF("NVRAM UPDATA!")
#else
#define NVRAM_UPDATA()   vid_nvram_updata_req()
#endif







#endif

